import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

export default class TimeManager {
    // 保存定时器
    static timers: { [key: string]: number } = {};

    // 设置一个定时器
    public static openPropTimer(id: string, event: Function, time: number){
        if (this.timers[id]) {
            clearTimeout(this.timers[id]);
        }
        // 创建新的计时器，并保存到 timers 对象中
        this.timers[id] = setTimeout(event, time);
    }
    
    // 关闭一个定时器
    public static closePropTimer(id: string){
        if (this.timers[id]) {
            clearTimeout(this.timers[id]);
            delete this.timers[id]; // 清除存储的计时器 ID
        }
    }
    
    // 关闭全部定时器
    public static closeAllPropTimer(){
        for (const id in this.timers) {
            if (this.timers[id]) {
                clearTimeout(this.timers[id]);
                delete this.timers[id]; // 清除存储的计时器 ID
            }
        }
    }
}